package com.zllkj.mm.module.shopping.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zllkj.mm.R
import com.zllkj.mm.adapter.AfterSaleListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.model.entity.AfterSalesBean
import com.zllkj.mm.module.shopping.vm.ItemAfterSaleListVM
import kotlinx.android.synthetic.main.fragment_after_sales_list.view.*
import java.util.HashMap

class AfterSaleGoodsListFragment : BaseTitleFragment() {

    var datas = ArrayList<ItemAfterSaleListVM>()

    lateinit var adapter : AfterSaleListAdapter

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_after_sales_list
    }

    override fun loadData() {
        super.loadData()

        setCommonTitle("售后列表")


        adapter = AfterSaleListAdapter(R.layout.item_after_sale_list,datas)
        mRootView.rv_after_sales_list.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_after_sales_list.adapter = adapter
        addSubscribe(RetrofitHelper.getInstance().orderApi.getAfterSaleList((HashMap<String, Any>().apply {
            this["pageSize"] = 20
            this["currentPage"] = 1
        })).compose<AfterSalesBean>(RxHelper.defaultHandleResult<AfterSalesBean>()).subscribe(object : ProgressSubscriber<AfterSalesBean>(context){
            override fun onSucc(t: AfterSalesBean?) {
                t?.records?.map {
                    var vm = ItemAfterSaleListVM()
                    vm.afterSaleId = it.afterSaleId.toString();
                    vm.afterSaleNo = "订单编号:" +it.afterSaleNo
                    vm.name = it.shopName
                    vm.goodsPic = it.afterSaleGoodsList[0].goodsPic
                    vm.orderId = it.orderId.toString()
                    vm.goodsTitle = it.afterSaleGoodsList[0].goodsName
                    vm.goodsCount = it.afterSaleGoodsList[0].buyCount.toString() + "件"
                    vm.goodsUnitPrice = it.afterSaleGoodsList[0].unitPrice.toString()
                    datas.add(vm)

                }
                adapter.notifyDataSetChanged()
            }
        }))

        adapter.setOnItemChildClickListener(object:BaseQuickAdapter.OnItemChildClickListener{
            override fun onItemChildClick(p0: BaseQuickAdapter<*, *>?, p1: View?, p2: Int) {
                addFragment(AfterSaleGoodsDetailFragment().apply {
                    arguments = Bundle().apply { putString("orderId",datas[p2].afterSaleId) }
                })
            }
        })
    }
}